package com.starblink.library.widget.subscription

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import androidx.core.os.bundleOf
import androidx.lifecycle.lifecycleScope
import com.starblink.android.basic.base.activity.AbsActivity
import com.starblink.android.basic.network.base.SkGqlClient
import com.starblink.rocketreserver.FetchSubscriptionSettingQuery
import kotlinx.coroutines.launch

fun Context.showSubscribeDialog() {
    (this as? AppCompatActivity)?.run {
        lifecycleScope.launch {
            var registerEmail: String? = null
            val email = SkGqlClient.skQuery(
                FetchSubscriptionSettingQuery(),
                showDefaultLoading = true
            )?.data?.fetchSubscriptionSetting?.subscriptionSettingRespF?.let {
                registerEmail = it.registerEmail
                it.email?.ifEmpty { it.registerEmail }
            }
            SubsEmailSetDF().apply {
                subsEmailVal = email
                this.registerEmail = registerEmail
            }.show(supportFragmentManager, SubsEmailSetDF::javaClass.name)
        }
    }
}

fun Context.showSubsEmailInputDialog(email: String? = null, dialogHeight:Int?=null, success: ()-> Unit) {
    (this as? AppCompatActivity)?.run {
        lifecycleScope.launch {
            SubsEmailInputDF(dialogHeight).apply {
                if (email != null) {
                    arguments = bundleOf(
                        "email" to email
                    )
                }
                this.success = success
            }.show(supportFragmentManager, SubsEmailSetDF::javaClass.name)
        }
    }
}